package jjoms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import jjoms.aop.model.GetRankService;
import jjoms.model.info.InfoService;
import jjoms.model.info.InfoVO;
import jjoms.userException.UpdatedHitsInfoException;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/info")
public class InfoController {
	@Resource(name = "infoServiceImpl")
	private InfoService infoService;
	@Resource(name = "getRankServiceImpl")
	private GetRankService  getRankServiceImpl;
	/**
	 * 사용자의 검색어에 따른 정보의 유 / 무 에 따라 해당 jsp 로 이동한다.
	 * @param keyword
	 * @return
	 */
	@RequestMapping("/findByKeyword.moja")
	public ModelAndView findByKeyword(String keyword) {
		System.out.println("findByKeyword 시작 keyword : " + keyword);
		ModelAndView mv = null;
		InfoVO infoVO = null;
		try{
			infoVO = infoService.findByKeyword(keyword);
			mv = new ModelAndView("insertConfirm.info", "searchKeyword", keyword);
		}catch(UpdatedHitsInfoException e){ // 정보검색
			infoVO = e.getInfoVO();
			mv = new ModelAndView("findByKeyword_result.info", "infoVO", infoVO);
			System.out.println(e.getInfoVO());
		}
		return mv;
	}

	@RequestMapping("/getCategoryKind.moja")
	public ModelAndView getCategoryKind() {
		List<String> categoryList = infoService.getCategoryKind();
		return new ModelAndView("infoRegister_view.info", "categoryList", categoryList);
	}
	
	/**
	 * 해당 카테고리에 따른 중분류 Ajax
	 * @param request
	 * @return
	 */
	@RequestMapping("/getDivisionKind.moja")
	@ResponseBody
	public List<String> getDivisionKind(HttpServletRequest request) {
		String category = request.getParameter("category");
		return infoService.getDivisionKind(category);
	}

	@RequestMapping("/register.moja")
	public ModelAndView register(InfoVO infoVO) {
		infoService.infoRegiseter(infoVO);
	    infoService.infoCategory(infoVO);
	    return new ModelAndView("redirect:findByKeyword.moja?keyword="+infoVO.getKeyword());

	 }
	
	/**
	 * 정보 수정
	 * @param keyword
	 * @return
	 */
	@RequestMapping("/infoUpdate_view.moja")
	public ModelAndView infoUpdate(String keyword){
		InfoVO infoVO = null;
		List<String> categoryList = new ArrayList<String>();
		ModelAndView mv = null;
		System.out.println("infoUpdate ="+keyword);
		
			infoVO = infoService.findByKeywordNoException(keyword);
			System.out.println("infoVO"+ infoVO);

			categoryList = infoService.getCategoryKind();
			System.out.println("categoryList : " +categoryList);
			System.out.println("categoryList= size == "+ categoryList.size());
			for(int i = 0; i < categoryList.size(); i++){
				if(categoryList.get(i).equals(infoVO.getCategory())){
					categoryList.remove(i);
				}
			}

			mv = new ModelAndView("infoUpdate_view.info");
			mv.addObject("infoVO",infoVO);
			mv.addObject("categoryList", categoryList);
			System.out.println("바뀐 목록 : " + categoryList);
		
		return mv;
	}
	
	@RequestMapping("/infoUpdateResult.moja")
	public ModelAndView infoUpdateResult(InfoVO infoVO, HttpServletRequest request){
		String beforeKeyword = request.getParameter("beforeKeyword");
		infoService.infoUpdateResult(infoVO, beforeKeyword);
		return new ModelAndView("findByKeyword_result.info", "infoVO", infoVO);
	}
	
	@RequestMapping("/infoDelete.moja")
	public ModelAndView infoDelete(String keyword) {
		InfoVO infoVO = null;
		
			infoVO = infoService.findByKeywordNoException(keyword);
		
			if(infoVO.getSection() != null){
				infoService.deleteSection(keyword);
			}else if(infoVO.getDivision() != null){
				infoService.deleteDivision(keyword);				
			}
			
			infoService.infoDelete(keyword);
			getRankServiceImpl.deleteRank(keyword);
		return new ModelAndView("redirect:/member/home.moja");
	}
	
	@RequestMapping("/board.moja")
	public ModelAndView board(String category) {
		List<String> list = infoService.getDivisionKind(category);
		List<String> clist = infoService.getSection(category);
		ModelAndView mv = new ModelAndView("findByCategory.info");
		mv.addObject("list", list);
		mv.addObject("clist", clist);
		System.out.println("board");
		return mv;
	}
}
